﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common_Code.Math_Extensions
{
    public class Lagrange_Polynomial : Interpolation<double>
    {
        List<double> x;
        List<double> y;
        double[] func;
        
        public Lagrange_Polynomial()
        {
            this.x = new List<double>();
            this.y = new List<double>();
        }

        public Lagrange_Polynomial(double[] x, double[] y)
        {
            if (x.Length != y.Length)
                throw new NotSupportedException("The length's have to be the same");
            this.x = new List<double>();
            this.y = new List<double>();
            this.x.AddRange(x);
            this.y.AddRange(x);
            genLagrange();
        }

        private void genLagrange()
        {
            if (this.x.Count > 2)
            {
                func = new double[this.x.Count];
                var temp = new double[this.x.Count][];
                List<double> tempX;
                for (var i = 0; i < this.x.Count; i++)
                {
                    tempX = this.x;
                    x.RemoveAt(i);
                    temp[i] = genLagrange(x, this.y[i]).ToArray();
                }
                func = temp[0];
                for (var i = 1; i < this.x.Count; i++)
                {
                    for (var count = 0; i < this.x.Count; count++)
                    {
                        func[count] = func[count] + temp[i][count];
                    }
                }
            }
        }

        private List<double> genLagrange(List<double> x, double y)
        {
            var temp = new List<double>();
            for (var i = 0; i < x.Count; i++)
            {

            }
            throw new NotImplementedException();
        }

        public double GetInterpolationAt(double val)
        {
            throw new NotImplementedException();
        }

        public void AddPoint(double x, double y)
        {
            this.x.Add(x);
            this.y.Add(y);
            if (this.x.Count > 1)
            {
                genLagrange();
            }
        }

        public void AddPoints(double[] x, double[] y)
        {
            if (x.Length != y.Length)
                throw new NotSupportedException("The length's have to be the same");
            this.x.AddRange(x);
            this.y.AddRange(x);
            genLagrange();
        }
    }
}
